package Controller;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import POJO.*;
import Model.*;


/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserPOJO user  =  (UserPOJO)session.getAttribute("user");
		
		if(user != null)
		{
			//Đã đăng nhập
			String url = "";
			switch (user.getType())
			{
				case "Customer Service":
					url = "customer";
					break;
					
				case "Merchandise":
					url = "category";
					break;
					
				case "Accountant":
					url = "order";
					break;
					
				case "System Admin":
					url = "category";
					break;
			}
			response.sendRedirect(url);
		}
		else
		{
			//Chưa đăng nhập
			if(DataProvider.Successful())
			{
				if(request.getParameter("error")!= null)
				{
					request.setAttribute("error", "");
				}
				
				RequestDispatcher rd = request.getRequestDispatcher("View/Login.jsp");
				rd.forward(request, response);
				
			}
			else
			{
			
				Writer w = response.getWriter();
				w.write("cannot connect to database");
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//kiem tra dang nhap - phan quyen
		String username = (String)request.getParameter("Username");
		String password = (String)request.getParameter("Password");
		UserPOJO user = UserModel.selectUserByUserNamePassword(username, password);
		if(user != null)
		{
			//trương hop dăng nhập thành công
			//Tạo session
			HttpSession session = request.getSession();
			
			//set session thoog tin user 
			session.setAttribute("user", user);
			
			//set session menu
			String[][] arrMenu = new String[5][3];
			arrMenu[0] = new String[] {"Categories","category", ""};
			arrMenu[1] = new String[] {"Products","product", ""};
			arrMenu[2] = new String[] {"Orders","order", ""};
			arrMenu[3] = new String[] {"Customers","customer", ""};
			arrMenu[4] = new String[] {"Users","user", ""};

			String url = "";
			switch (user.getType())
			{
				case "Customer Service":
					arrMenu[3][2] = "true";
					url = "customer";
					break;
					
				case "Merchandise":
					arrMenu[0][2] = "true";
					arrMenu[1][2] = "true";
					url = "category";
					break;
					
				case "Accountant":
					arrMenu[2][2] = "true";
					url = "order";
					break;
					
				case "System Admin":
					arrMenu[0][2] = "true";
					arrMenu[1][2] = "true";
					arrMenu[2][2] = "true";
					arrMenu[3][2] = "true";
					arrMenu[4][2] = "true";
					url = "category";
					break;
			}
			session.setAttribute("menu", arrMenu);
			response.sendRedirect(url);
		}
		else
		{
			//không thành công - ve lại login
			response.sendRedirect("login?error");
		}
	}
}
